//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.utils.basehelper.date;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;
import java.util.TimeZone;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DateTimeUtils {
    private static final Logger log = LoggerFactory.getLogger(DateTimeUtils.class);
    public static final String YYYYMMDDHHMMSS = "yyyy-MM-dd HH:mm:ss";

    public DateTimeUtils() {
    }

    public static String date2String() {
        TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        df.setTimeZone(tz);
        String nowAsIso = df.format(new Date());
        return nowAsIso;
    }

    public static Long betweenSeconds(String startTime, String endTime) {
        if (!StringUtils.isBlank(startTime) && !StringUtils.isBlank(endTime)) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            try {
                Date startDate = dateFormat.parse(startTime);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(startDate);
                long timeInMillis1 = calendar.getTimeInMillis();
                Date endDate = dateFormat.parse(endTime);
                calendar.setTime(endDate);
                long timeInMillis2 = calendar.getTimeInMillis();
                long betweenSeconds = (timeInMillis2 - timeInMillis1) / 1000L;
                return betweenSeconds;
            } catch (ParseException var12) {
                log.error("betweenSeconds 计算给定日期Date间秒数异常，异常信息为：{}", var12.getMessage());
                return 0L;
            }
        } else {
            return 0L;
        }
    }

    public static Long betweenSeconds(Date startDate, Date endDate) {
        if (!Objects.isNull(startDate) && !Objects.isNull(endDate)) {
            try {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(startDate);
                long timeInMillis1 = calendar.getTimeInMillis();
                calendar.setTime(endDate);
                long timeInMillis2 = calendar.getTimeInMillis();
                long betweenSeconds = (timeInMillis2 - timeInMillis1) / 1000L;
                return betweenSeconds;
            } catch (Exception var9) {
                log.error("betweenSeconds 计算给定日期Date间秒数异常，异常信息为：{}", var9.getMessage());
                return 0L;
            }
        } else {
            return 0L;
        }
    }

    public static Long getRemainMilliSecondsOneDay(Date currentDate) {
        if (Objects.isNull(currentDate)) {
            return 0L;
        } else {
            Calendar midnight = Calendar.getInstance();
            midnight.setTime(currentDate);
            midnight.add(5, 1);
            midnight.set(11, 0);
            midnight.set(12, 0);
            midnight.set(13, 0);
            midnight.set(14, 0);
            Long milliSeconds = midnight.getTime().getTime() - currentDate.getTime();
            return milliSeconds;
        }
    }

    public static Long betweenMillisSeconds(Date startDate, Date endDate) {
        if (!Objects.isNull(startDate) && !Objects.isNull(endDate)) {
            try {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(startDate);
                long timeInMillis1 = calendar.getTimeInMillis();
                calendar.setTime(endDate);
                long timeInMillis2 = calendar.getTimeInMillis();
                long betweenMillisSeconds = timeInMillis2 - timeInMillis1;
                return betweenMillisSeconds;
            } catch (Exception var9) {
                log.error("betweenSeconds 计算给定日期Date间秒数异常，异常信息为：{}", var9.getMessage());
                return 0L;
            }
        } else {
            return 0L;
        }
    }

    public static String getYesterday(String queryTime) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
        Date date = null;

        try {
            date = sf.parse(queryTime);
        } catch (ParseException var6) {
            var6.printStackTrace();
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int day1 = calendar.get(5);
        calendar.set(5, day1 - 1);
        String newDay = sf.format(calendar.getTime());
        return newDay;
    }

    public static String getYesterWeek(String queryTime) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyyww");
        Date date = null;

        try {
            date = sf.parse(queryTime);
        } catch (ParseException var7) {
            var7.printStackTrace();
        }

        Date agoDayByDate = com.zr.test.DateUtils.getAgoDayByDate(date, -7);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(agoDayByDate);
        int week = calendar.get(3);
        int year = calendar.getWeekYear();
        return String.valueOf(year) + (week < 10 ? "0" + week : week);
    }

    public static String getYesterMonth(String queryTime) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMM");
        Date date = null;

        try {
            date = sf.parse(queryTime);
        } catch (ParseException var6) {
            var6.printStackTrace();
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int day1 = calendar.get(2);
        calendar.set(2, day1 - 1);
        String newDay = sf.format(calendar.getTime());
        return newDay;
    }

    public static String getYesterYear(String queryTime) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy");
        Date date = null;

        try {
            date = sf.parse(queryTime);
        } catch (ParseException var6) {
            var6.printStackTrace();
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int day1 = calendar.get(1);
        calendar.set(1, day1 - 1);
        String newDay = sf.format(calendar.getTime());
        return newDay;
    }

    public static void main(String[] args) {
        SimpleDateFormat sd = new SimpleDateFormat("dd");
        SimpleDateFormat smd = new SimpleDateFormat("MMdd");
        Date date = new Date();
        System.out.println(Objects.equals("01", sd.format(date)));
        System.out.println(Objects.equals("0101", smd.format(date)));
    }
}
